﻿#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using System.Collections.Generic;

public static class LoadTools
{
    static private AssetBundleManifest _manifest;

    static private Dictionary<string, AssetBundle> _dicAssetBundle;
    static private Dictionary<string, int> _dicAssetBundleUsed;

    static public void Init()
    {
        Debug.Log("【LoadTools——Init】");
#if UNITY_EDITOR
        if (GameConfig.useAssetBundle == false)
            return;
#endif
        if (_manifest == null)
        {
            _dicAssetBundle = new Dictionary<string, AssetBundle>();
            _dicAssetBundleUsed = new Dictionary<string, int>();
        }
    }

    static private AssetBundle Load(string name)
    {
        Debug.Log("name " + name);
        return AssetBundle.LoadFromFile(GameConfig.assetBundlePath + "/" + name);
    }

    static public AssetBundle LoadAssetBundle(string name)
    {
        name = name.ToLower();

        if (_dicAssetBundle.ContainsKey(name) == false)
        {
            _dicAssetBundle[name] = Load(name);
            _dicAssetBundleUsed[name] = 0;
        }

        string[] arrNames = _manifest.GetAllDependencies(name);

        for (int i = 0; i < arrNames.Length; i++)
        {
            if (_dicAssetBundle.ContainsKey(arrNames[i]) == true)
                continue;
            else
                LoadAssetBundle(arrNames[i]);
        }

        _dicAssetBundleUsed[name]++;
        return _dicAssetBundle[name];
    }

    static public GameObject LoadPrefab(string moudule, string name)
    {
        string path = moudule + "/" + name;

#if UNITY_EDITOR
        if (GameConfig.useAssetBundle == false)
        {
            return AssetDatabase.LoadAssetAtPath<GameObject>("Assets/BundleResources/" + path + ".prefab");
        }
#endif

        return LoadAssetBundle(moudule).LoadAsset<GameObject>(name);
    }

    static public GameObject LoadGameObject(string moudule, string name, GameObject parent = null)
    {
        GameObject source = LoadPrefab(moudule, name);
        if (source == null)
        {
            Debug.LogError("Error not find " + moudule + " " + name);
            return null;
        }

        GameObject obj = GameObject.Instantiate(source) as GameObject;
        obj.name = source.name;
        if (parent != null)
        {
            obj.transform.SetParent(parent.transform);
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localRotation = Quaternion.identity;
            obj.transform.localScale = Vector3.one;
            obj.layer = parent.layer;
        }
        return obj;
    }

    static public Sprite LoadSprite(string path, string name)
    {
#if UNITY_EDITOR
        if (GameConfig.useAssetBundle == false)
        {
            return AssetDatabase.LoadAssetAtPath<Sprite>("Assets/BundleResources/" + path + "/" + name + ".png");
        }
#endif
        return LoadAssetBundle(path).LoadAsset<Sprite>(name);
    }

    static public T Load<T>(string path, string name) where T : Object
    {
        return LoadAssetBundle(path).LoadAsset<T>(name);
    }
}


